package feeinterface.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ulwx.database.DbException;
import com.ulwx.tool.DbUtils;
import feeinterface.modeldb.BaseThirdPartyURL;

public class BaseThirdPartyURLDao {

	public static BaseThirdPartyURL findBy(Integer id) throws Exception {
		String sql = "select * from BaseThirdPartyURL where "
				+ "BsThirdPartyURLSno=?";
		Map<Integer, Object> map = new HashMap<Integer, Object>();
		map.put(1, id);
		List<BaseThirdPartyURL> list = DbUtils.doQueryClass(null,
				BaseThirdPartyURL.class, sql, map);
		if (list.size() >= 1)
			return list.get(0);

		return null;

	}

	/**
	 * 取得第三方获取用户手机号码的URL
	 * 
	 * @return
	 * @throws DbException
	 */
	public static BaseThirdPartyURL getThirdPartyUrl() throws Exception {
		BaseThirdPartyURL url = null;
		String sql = " SELECT * FROM BaseThirdPartyURL WHERE ENABLE = 1 limit 1";
		Map<Integer, Object> map = new HashMap<Integer, Object>();
		List<BaseThirdPartyURL> list = DbUtils.doQueryClass(null,
				BaseThirdPartyURL.class, sql, map);
		if (list != null && list.size() > 0) {
			url = list.get(0);
			// 如果有找到，就更新该地址的使用次数
			sql = "UPDATE BaseThirdPartyURL SET UseCnt = UseCnt + 1 WHERE BsThirdPartyURLSno = ?";
			map.clear();
			map.put(1, url.getBsThirdPartyURLSno());
			DbUtils.executeBindUpdate(null, sql, map);
		}
		return url;
	}
}
